package com.xiaou.buss.admin.rolemenu.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.criteria.Criteria;
import com.base.service.BaseServiceImpl;
import com.xiaou.buss.admin.rolemenu.dao.RoleMenuDao;
import com.xiaou.buss.admin.rolemenu.entity.RoleMenu;
import com.xiaou.buss.admin.rolemenu.service.RoleMenuService;

/**
 * 
 * <br>
 * <b>功能：</b>RoleMenuService<br>
 */
@Service("roleMenuService")
public class  RoleMenuServiceImpl  extends BaseServiceImpl implements RoleMenuService {
  Logger logger = LoggerFactory.getLogger(getClass());
	

	@Autowired
    private RoleMenuDao dao;

		
	public RoleMenuDao getDao() {
		return dao;
	}


	@Override
	@Transactional
	public void addMenu(String roleId, String menuIds) {
		String[] mids = null;;
		if(StringUtils.isNotBlank(menuIds)){
			mids=menuIds.split(",");
		}
		
		//先删除所有的菜单
		Criteria cta = new Criteria();
		cta.put("roleId", roleId);
		List<RoleMenu> roleMenus = dao.queryByList(cta);
		for (RoleMenu roleMenu : roleMenus) {
			logger.info("roleMenus:"+roleMenus.size());
			logger.info(roleMenu.getRoleId());
			dao.delete(roleMenu);
			
		}
		for (String menuId : mids) {
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setMenuId(menuId);
			roleMenu.setRoleId(roleId);
			dao.add(roleMenu);
		}
		
	}


	@Override
	public void delByMenuId(Integer menuId) {
		dao.delByMenuId(menuId);
	}


	

}
